package top.lvkun.basic.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.lvkun.basic.mapper.BaseMapper;
import top.lvkun.basic.query.BaseQuery;
import top.lvkun.basic.service.IBaseService;
import top.lvkun.basic.utils.PageList;

import java.util.List;

//不需要加@Service,子类继承是添加
//类级别事务为读事务,分别在增删改的方法上面加写事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    //    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    @Transactional
    @Override
    public void add(T t) {
        baseMapper.save(t);
//        int i = 1/0;
    }

    @Transactional
    @Override
    public void del(Long id) {
        baseMapper.remove((Long) id);
    }
    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T getById(Long id) {
        return baseMapper.loadById((Long) id);
    }

    @Override
    public List<T> getAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> queryData(BaseQuery query) {
        Integer count = baseMapper.queryCount(query);
        System.out.println(count);
        List<T> rows = baseMapper.queryData(query);
        return new PageList<T>(count,rows);
    }

    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }
}